package cn.larry.tools;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import cn.larry.tools.utils.DomainGenUtil;
import cn.larry.tools.utils.HTTPClientUtil;

public class DomainCheck {

	public static void main(String[] args) throws IOException,
			InterruptedException {

		String url = "http://pandavip.www.net.cn/check/check_ac1.cgi";
		String start;
		String end;
		List<String> list = new ArrayList(0);
		
		String sufix = "com";
		if (args != null && args.length == 4) {
			start = args[2];
			end = args[3];
			sufix = args[1];
			if (args[0] != null && args[0].equalsIgnoreCase("ZiMu4")) {
				list = DomainGenUtil.genZimu4(start, end);
			} else if (args[0] != null && args[0].equalsIgnoreCase("ZiMu3")) {
				list = DomainGenUtil.genZimu3(start, end);
			} else if (args[0] != null && args[0].equalsIgnoreCase("Shuzi3")) {
				list = DomainGenUtil.genShuzi3();
			} else if (args[0] != null && args[0].equalsIgnoreCase("Shuzi4")) {
				list = DomainGenUtil.genShuzi4();
			} else if (args[0] != null && args[0].equalsIgnoreCase("Pinyin1")) {
				list = DomainGenUtil.genPinyin1();
			} else if (args[0] != null && args[0].equalsIgnoreCase("Pinyin2")) {
				list = DomainGenUtil.genPinyin2();
			} else {
				list = DomainGenUtil.genZimu4(start, end);
			}
		} else {
			System.err.println("args参数数目不对！Zimu4 com 'a' 'z' ");
		}
		System.out.println("args[0]->" + args[0] != null ? args[0] : null);
		int len = 5;
		RandomAccessFile raf = new RandomAccessFile("domains.txt", "rw");
		System.out.println(new Date());
		for (int i = 0; i < list.size() / len; i++) {
			StringBuilder sb = new StringBuilder();

			for (int j = 0; j < len; j++) {
				sb.append(list.get(i * len + j) + "." + sufix + ",");
			}

			sb.deleteCharAt(sb.length() - 1);
			System.out.println(sb);
			Thread.sleep(100);
			List<NameValuePair> nvps = new ArrayList<NameValuePair>();
			nvps.add(new BasicNameValuePair("domain", sb.toString()));

			fire(url, nvps, raf);

		}
		raf.close();
		System.out.println(new Date());
	}

	private static void fire(String url, List<NameValuePair> nvps,
			RandomAccessFile raf) throws IOException {
		String result = HTTPClientUtil.HTTPPost(url, nvps);
		System.out.println("return:" + result);

		if (result != null) {
			String[] array = result.split("#");
			for (String item : array) {
				if (item.contains("|210|")) {
					System.out.println("OK:" + item);
					raf.write((item + "\n").getBytes());
				} else {
					// System.out.println("NO:"+item);
				}
			}
		}

	}

}
